<template>
  <div class="mod-config">
    <table-view ref="view"
                :searchAttrs="searchAttrs"
                :primaryKey="primaryKey"
                :listApiName="listApiName"
                :deleteApiName="deleteApiName"
                :saveAuthName="saveAuthName"
                :deleteAuthName="deleteAuthName"
                :showAttrs="showAttrs"
    >
    </table-view>

  </div>
</template>

<script>
import AddOrUpdate from './dayproductcount-add-or-update'
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import lineChart from '@/views/charts/day/dayProductCountLineChart'
import pieChart from '@/views/charts/day/dayProductCountPieChart'
import {mRequest} from '@/utils/request'

export default {
  mixins: [basicMixins],
  data() {
    return {
      chartSearchObj: {
        dataForm: {
          dateTimeRange: []
        },
        config: {
          dateTimeRange: {
            type: 'date',
            clearable: true,
            dateType: 'daterange',
            label: '时间范围'
          }
        }
      },
      dayProductCountVisible: false,
      productDeviceList: [],
      deviceIdList: [],
      orderDetailList: [],
      productInfoList: [],
      userInfoList: [],
      tableListData: [{
        orderDetailId: '',
        productId: '',
        classId: '',
        userId: '',
        successRate: 0,
        countWeight: 0,
        productWeight: '',
        boxPieceNumber: '',
        productNumber: '',
        boxNumber: '',
        productionState: 0
      }],
      dayTime: '',
      deviceId: '',
      dripLiquid: 0,
      duration: 0,
      machineSpeed: 0,
      chartTypes: [
        {label: 'pie', text: '饼状图'},
        {label: 'line', text: '折线图'}
      ],
      searchAttrs: {
        classId: {
          type: 'autoComplete',
          placeholder: '班次',
          querySearchApiName: 'userApi/getUserInfoList'
        },
        productId: {
          type: 'autoComplete',
          placeholder: '请选择产品',
          querySearchApiName: 'productApi/getProductInfoList'
        },
        deviceId: {
          type: 'autoComplete',
          placeholder: '请选择设备',
          querySearchApiName: 'productApi/getProductDeviceList'
        },
        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        }
      },
      primaryKey: 'id',
      listApiName: 'dayApi/listDayProductList',
      deleteApiName: 'dayApi/deleteDayProductCount',
      saveAuthName: 'day:dayproductcount:save',
      deleteAuthName: 'day:dayproductcount:delete',
      showAttrs: {
        deviceName: {label: '料道'},
        classesName: {label: '班别'},
        userName: {label: '员工'},
        productNo: {label: '产品名称'},
        dayShift:{label: '白班合格率'},
        middleShift: {label: '中班合格率'},
        nightShift: {label: '晚班合格率'},
        productionState:{
          label:'生产状态'
        },
        outputCountWeight: {label: '出料量(t)'},
        successRate: {label: '成品率'},
        dayTime: {label: '生产日期', type: 'date'},
        daySuccessRate: {label: '本日合格率'}
      }
    }
  },
  components: {
    AddOrUpdate,
    tableView,
    lineChart,
    pieChart
  },
  methods:{
    multiplyDayProductCount(){
      this.dayProductCountVisible = true
      this.tableListData = [{
        orderDetailId: '',
        productId: '',
        classId: '',
        userId: '',
        successRate: 0,
        countWeight: 0,
        productWeight: '',
        boxPieceNumber: '',
        productNumber: '',
        boxNumber: '',
        productionState: 0
      }]
      this.dayTime = ''
      this.deviceId = ''
      this.dripLiquid = 0
      this.duration = 0
      this.machineSpeed = 0
      this.initProductDeviceList()
      this.initOrderDetailList()
      this.initProductInfoList()
      this.initUserInfoList()

    },
    deleteRow(index) {
      this.tableListData.splice(index, 1);
    },
    addRow(index) {
      let _object = {
        orderDetailId: '',
        productId: '',
        classId: this.tableListData[index].classId,
        userId: this.tableListData[index].userId,
        successRate: 0,
        countWeight: 0,
        productWeight: '',
        boxPieceNumber: '',
        productNumber: '',
        boxNumber: '',
        productionState: 0
      }
      this.tableListData.push(_object);
    },
    initProductDeviceList() {
      mRequest('productApi/getProductDeviceList', {}, (res) => {
        this.productDeviceList= this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initUserInfoList() {
      mRequest('userApi/getUserInfoList', {}, (res) => {
        this.userInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initOrderDetailList(){
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initProductInfoList() {
      mRequest('productApi/getProductInfoList', {}, (res) => {
        this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    funcAttrGet(orderDetailId,index){
      mRequest('orderApi/orderDetailInfo', {id: orderDetailId}, ({data}) => {
        this.tableListData[index].productId = this.R.pathOr(0, ['orderDetail', 'productId'], data)
        this.tableListData[index].productWeight = this.R.pathOr(0, ['orderDetail', 'productWeight'], data)
      })

    },
    changeNumber(index){
      if (0 !== this.tableListData[index].productNumber) {
        this.tableListData[index].countWeight= this.tableListData[index].productWeight *  this.tableListData[index].productNumber
      }else {
        this.tableListData[index].productNumber= this.tableListData[index].boxPieceNumber * this.tableListData[index].boxNumber
        this.tableListData[index].countWeight= this.tableListData[index].productWeight *  this.tableListData[index].productNumber
      }
      if (0 != this.dripLiquid && 0 != this.duration) {
        this.machineSpeed = this.dripLiquid / this.duration
      }
      if (0 != this.machineSpeed) {
        this.tableListData[index].successRate= this.tableListData[index].productNumber / (this.machineSpeed * 60 * 8) * 100
      }
    },
    changeDripLiquid(){
      if (0 != this.dripLiquid && 0 != this.duration) {
        this.machineSpeed = this.dripLiquid / this.duration
      }
      if (0 != this.machineSpeed) {
        for (let index = 0; index <this.tableListData.length ; index++) {
          this.tableListData[index].successRate= this.tableListData[index].productNumber / (this.machineSpeed * 60 * 8) * 100

        }
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
          this.dayProductCountVisible = false
        })
        .catch(_ => {});
    },
    multiplySaveDayProduction(){
      mRequest('dayApi/multiplySaveDayProduction', {
        'dayTime': this.dayTime,
        'deviceId': this.deviceId,
        'dripLiquid': this.dripLiquid,
        'duration': this.duration,
        'machineSpeed': this.machineSpeed,
        'tableListData': this.tableListData
      }, (res => {
        let data = res.data
        if (data && data.code == 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.dayProductCountVisible = false
              this.getDataList()
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      }))
    }
  }
}
</script>
